package org.itboys.mobile.controller.web.pos;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.kitchen.dao.order.KitchenOrderMapper;
import org.itboys.kitchen.dao.order.KitchenOrderProductMapper;
import org.itboys.kitchen.entity.mysql.order.KitchenOrder;
import org.itboys.kitchen.entity.mysql.order.KitchenOrderProduct;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.mobile.service.mysql.web.promotion.SundayPromotionService;
import org.itboys.mobile.service.pos.PosOrderService;
import org.itboys.mobile.service.pos.PosProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商品_controller_web
 *
 */
@Controller
@RequestMapping("/sunday/web/pos")
public class SundayPosController extends BaseController {

    @Autowired
    private PosProductService posProductService;
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private KitchenOrderMapper orderMapper;
    @Autowired
    private KitchenOrderProductMapper orderProductMapper;
    @Autowired
    private PosOrderService posOrderService;
    /**
     * 同步pos和APP的商品，名称，规格，商品编码；商品价格和库存请在门店管理中同步
     * @param request
     * @param response
     */
    @RequestMapping("/processProductToApp")
    public  void processProductToApp(HttpServletRequest request,
                      HttpServletResponse response
    ) {
        try {
            posProductService.processProductToApp();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/pos/processProductToApp------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }
    }
    /**
     * 向POS导入订单
     * @param request
     * @param response
     */
    @RequestMapping("/processOrderToPos")
    public  void processOrderToPos(HttpServletRequest request,
                                     HttpServletResponse response,
                                   @RequestParam(value="orderId",required = true)Long orderId
    ) {
        try {
                posOrderService.processOrderToPos(orderId);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/pos/processOrderToPos------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }
    }

}
